//
//  UserDetailInfoViewController.m
//  MemberProject
//
//  Created by macbook on 15/4/13.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "UserDetailInfoViewController.h"
#define SCR_HEIGHT [UIScreen mainScreen].applicationFrame.size.height
#define SCR_WIDTH  [UIScreen mainScreen].applicationFrame.size.width
#import "UserDetailInfoTableViewCell.h"
#import "ModelUser.h"
#import "UserInfoManager.h"
#import "ModelGroup.h"
#import "SVProgressHUD.h"
@interface UserDetailInfoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic)UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *lineLabel;
@property(strong,nonatomic)NSMutableArray *dataArr;

@property(nonatomic)BOOL volunInfoLoading;
@property(nonatomic)BOOL insuranceInfoLoading;

@end

@implementation UserDetailInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"个人信息";
    
    self.automaticallyAdjustsScrollViewInsets=NO;
    
    ModelUser *modelUser = [UserInfoManager sharedManager].modelMser;
    
    if (modelUser) {
        self.dataArr = [[NSMutableArray alloc]initWithObjects:@[@"姓名:",modelUser.userName==nil? @"":modelUser.userName],@[@"所属团组织:",@""],@[@"志愿者社团:",@""],@[@"志愿者卡号:",modelUser.voluteerId==nil? @"":modelUser.voluteerId],@[],@[@"注册志愿者服务意外保障:",@""],@[@"保单号:",@""],@[@"保单有效期:",@""],@[@"保单提供单位:",@""],nil];
    }else
    {
        self.dataArr=[[NSMutableArray alloc]initWithObjects:@[@"姓名:",@""],@[@"所属团组织:",@""],@[@"志愿者社团:",@""],@[@"志愿者卡号:",@""],@[],@[@"注册志愿者服务意外保障:",@""],@[@"保单号:",@""],@[@"保单有效期:",@""],@[@"保单提供单位:",@""],nil];
    }
//    self.dataArr=@[@[@"姓名:",modelUser.userName==nil? @"":modelUser.userName],@[@"所属团组织",@"A/N"],@[@"志愿者卡号:",modelUser.voluteerId==nil? @"":modelUser.voluteerId],@[@"所属志愿者机构",@"志愿者工作部"],@[],@[@"保单号:",@"A/N"],@[@"保单有效期:",@"A/n"],@[@"保单提供单位:",@"A/N"],@[],@[@"发放单位",@"地区服务号"],@[@"A/N",@"A/N"],@[@"A/N",@"A/N"]];
    UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(5, 64+6, SCREEN_WIDTH-10, (SCREEN_HEIGHT-20)/16*self.dataArr.count) style:UITableViewStylePlain];
    self.tableView=tableView;
//    [self setExtraCellLineHidden:self.tableView];
    if(SCREEN_HEIGHT<568&&self.dataArr.count>10){
        tableView.height=SCREEN_HEIGHT-49-tableView.y-10;
    }
    self.tableView=tableView;
    tableView.delegate=self;
    tableView.dataSource=self;
    [self.view addSubview:tableView];
    tableView.scrollEnabled=NO;
    tableView.layer.cornerRadius=5.0f;
    tableView.layer.borderWidth=0.5;
    tableView.layer.borderColor=[UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1].CGColor;
    
    [tableView registerNib:[UINib nibWithNibName:@"UserDetailInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"CellIdentifier"];
    
    if (modelUser) {
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeNone];
        [self refreshVolunInfo:modelUser];
        [self refreshInsuranceInfo:modelUser];
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}
#pragma mark 查询保单信息
-(void)refreshInsuranceInfo:(ModelUser *)modelUser
{
    /*
     responseData =     {
     insuranceId = 1;
     insuranceNo = PC0200A005402162;
     insuranceProcessDate = "<null>";
     payId = "<null>";
     periodEnd = "2016-04-15";
     periodStart = "2015-04-15";
     premium = 1;
     serviceProvider = GP0200A004997548;
     transactionBatch = "20150414-110067148001-080731";
     voluteerId = 38;
     };@[@"保单号:",@""],@[@"保单有效期:",@""],@[@"保单提供单位:",@""]
     */
    
    NSDictionary *parameters1 = @{@"token":@"sdfsadfsadfasdf",@"module":@"Voluteer",@"method":@"getVoluteerInsurance",@"requestId":[NSNull new],@"requestParam":@[modelUser.voluteerId]};
    
    //responseData
    [Utils requestDic:parameters1 andRusult:^(id result) {
        self.insuranceInfoLoading=NO;
        if(self.volunInfoLoading==YES){
            
        }else{
            [SVProgressHUD dismiss];
        }
        if ([result isKindOfClass:[NSDictionary class]])
        {
            NSDictionary *responseData = result[@"responseData"];
            if(![responseData isKindOfClass:[NSNull class]])
            {
                NSString *insuranceNo = responseData[@"insuranceNo"];
                NSString *insuranceTime = [NSString stringWithFormat:@"%@~%@",responseData[@"periodStart"],responseData[@"periodEnd"]];
                NSString *insurancePri = responseData[@"serviceProvider"];
                NSMutableArray *indexArr = [NSMutableArray new];
                for (int i=5; i<8; i++) {
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                    [indexArr addObject:indexPath];
                }
                [self.dataArr replaceObjectAtIndex:6 withObject:@[@"保单号:",insuranceNo]];
                [self.dataArr replaceObjectAtIndex:7 withObject:@[@"保单有效期:",insuranceTime]];
                [self.dataArr replaceObjectAtIndex:8 withObject:@[@"保单提供单位:",insurancePri]];
                [self.tableView reloadData];
                //                [self reloadIndexPath:indexArr];
                
            }
        }
    } andError:^(id result) {
        
    }];
    
    
}
#pragma mark 查找志愿者组织
-(void)refreshVolunInfo:(ModelUser *)modelUser
{
    //查找志愿者组织信息
    self.volunInfoLoading=YES;
    NSDictionary *parameters = @{@"token":@"sdfsadfsadfasdf",@"module":@"VoluteerGroup",@"method":@"findGroup",@"requestId":[NSNull new],@"requestParam":@[modelUser.groupId]};
    [Utils requestDic:parameters andRusult:^(id result) {
        NSDictionary *responseData = result[@"responseData"];
        if ([responseData isKindOfClass:[NSDictionary class]]) {
            ModelGroup *group = [ModelGroup getGroupForDic:responseData];
            if (group) {
                if (group.groupName) {
                    NSArray *groupNameArr = @[@"志愿者社团:",group.groupName];
                    [self.dataArr replaceObjectAtIndex:2 withObject:groupNameArr];
                    [self.tableView reloadData];
                    //                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
                    //                    [self reloadIndexPath:@[indexPath]];
                }
                if (group.orgId) {
                    [self refreshOrgInfo:[NSNumber numberWithInt:group.orgId.intValue]];
                }else{
                    self.volunInfoLoading=NO;
                    if(self.insuranceInfoLoading==YES){
                        
                    }else{
                        [SVProgressHUD dismiss];
                    }
                }
                //                if (group.orgName) {
                //                    NSArray *groupNameArr = @[@"所属团组织",group.groupName];
                //                    [self.dataArr replaceObjectAtIndex:1 withObject:groupNameArr];
                //                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
                //                    [self reloadIndexPath:indexPath];
                //                }
            }
            
            
        }
    } andError:^(id result) {
        
    }];
    
}
#pragma mark 查找团组织
-(void)refreshOrgInfo:(NSNumber *)orgId
{
    //查找团组织
    NSDictionary *parameters = @{@"token":@"sdfsadfsadfasdf",@"module":@"VoluteerOrg",@"method":@"findOrg",@"requestId":[NSNull new],@"requestParam":@[orgId]};
    [Utils requestDic:parameters andRusult:^(id result) {
        self.volunInfoLoading=NO;
        if(self.insuranceInfoLoading==YES){
            
        }else{
            [SVProgressHUD dismiss];
        }
        NSDictionary *responseData = result[@"responseData"];
        if ([responseData isKindOfClass:[NSDictionary class]]) {
            //更新数组
            NSString *orgName = responseData[@"orgName"];
            if (orgName) {
                NSArray *groupNameArr = @[@"所属团组织:",orgName];
                [self.dataArr replaceObjectAtIndex:1 withObject:groupNameArr];
                [self.tableView reloadData];
                //                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
                //                    [self reloadIndexPath:@[indexPath]];
            }
            //更新tableview长度
            //            if(SCREEN_HEIGHT>568){
            //                self.tableView.height=(SCREEN_HEIGHT-20)/16*self.dataArr.count;
            //            }else{
            //                self.tableView.height=SCREEN_HEIGHT-49-self.tableView.y-10;
            //            }
        }
    } andError:^(id result) {
        
    }];
}

#pragma mark 更新cell
-(void)reloadIndexPath:(NSArray *)indexPathArr
{
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self.tableView beginUpdates];
        [self.tableView reloadRowsAtIndexPaths:indexPathArr withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView endUpdates];
//    });
    
}
-(void)chickModifPasswordButton:(UIButton *)sender{
    //修改密码

}
#pragma mark - tableView
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(SCREEN_HEIGHT>568||self.dataArr.count<10){
        return (SCREEN_HEIGHT-20)/16;
    }else{
        return (SCREEN_HEIGHT-49-tableView.y-10)/self.dataArr.count;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier=@"CellIdentifier";
    UserDetailInfoTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.contentView.userInteractionEnabled = NO;
    if(SCREEN_HEIGHT>568){
        cell.keyLabel.font=[UIFont systemFontOfSize:17.0f];
        cell.valueLabel.font=[UIFont systemFontOfSize:17.0f];
    }else{
        cell.keyLabel.font=[UIFont systemFontOfSize:14.0f];
        cell.valueLabel.font=[UIFont systemFontOfSize:14.0f];
    }
    if(indexPath.row==5){
        cell.keyLabel.frame=CGRectMake(12, 0, 200,cell.height);
        cell.valueLabel.frame=CGRectMake(cell.keyLabel.x+cell.keyLabel.width, 0, (SCREEN_WIDTH-cell.keyLabel.x-cell.keyLabel.width), cell.height);
    }else{
        cell.keyLabel.frame=CGRectMake(12, 0, 120,cell.height);
        cell.valueLabel.frame=CGRectMake(cell.keyLabel.x+cell.keyLabel.width, 0, (SCREEN_WIDTH-cell.keyLabel.x-cell.keyLabel.width), cell.height);
    }
    NSArray *arr=self.dataArr[indexPath.row];
    if(arr.count==0){
        cell.keyLabel.hidden=YES;
        cell.valueLabel.hidden=YES;
    }else{
        cell.keyLabel.hidden=NO;
        cell.valueLabel.hidden=NO;
        cell.keyLabel.text=arr[0];
        cell.valueLabel.text=arr[1];
    }
    return cell;
}
@end
